<?php

namespace post\modules\user\models;

use post\modules\post\models\Post;
use Yii;
use yii\behaviors\TimestampBehavior;
use yii\base\NotSupportedException;
use yii\web\IdentityInterface;


class User extends \yii\db\ActiveRecord implements IdentityInterface
{
    
    public static function tableName()
    {
        return '{{%user}}';
    }

    const STATUS_DELETED = 0;
    const STATUS_ACTIVE = 10;
   public function behaviors()  
    {  
        return [  
            [
//                TimestampBehavior
                'class' => TimestampBehavior::className(),
                'attributes' => [
                    \yii\db\ActiveRecord::EVENT_BEFORE_INSERT => ['created_at','updated_at'],
                    \yii\db\ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at']
                ],  
            ],  
        ];  
    }

    public  function  getInfo()
    {
        return $this->hasOne(UserInfo::className(),['user_id'=>'id']);
    }


    /**
     * Finds user by username
     *
     * @param string $username
     * @return static|null
     */
    public static function findByUsername($username)
    {
        return static::findOne(['username' => $username, 'status' => self::STATUS_ACTIVE]);
    }

    /**
     * Finds an identity by the given ID.
     * @param string|int $id the ID to be looked for
     * @return IdentityInterface the identity object that matches the given ID.
     * Null should be returned if such an identity cannot be found
     * or the identity is not in an active state (disabled, deleted, etc.)
     */
    public static function findIdentity($id)
    {
        return static::findOne(['id' => $id, 'status' => self::STATUS_ACTIVE]);
    }

    /**
     * Finds an identity by the given token.
     * @param mixed $token the token to be looked for
     * @param mixed $type the type of the token. The value of this parameter depends on the implementation.
     * For example, [[\yii\filters\auth\HttpBearerAuth]] will set this parameter to be `yii\filters\auth\HttpBearerAuth`.
     * @return IdentityInterface the identity object that matches the given token.
     * Null should be returned if such an identity cannot be found
     * or the identity is not in an active state (disabled, deleted, etc.)
     */
    public static function findIdentityByAccessToken($token, $type = null)
    {

        return static::findOne ( ['access_token' => $token] );
//        throw new NotSupportedException('"findIdentityByAccessToken" is not implemented.');
    }


    /**
     * Returns an ID that can uniquely identify a user identity.
     * @return string|int an ID that uniquely identifies a user identity.
     */
    public function getId()
    {
        return $this->getPrimaryKey();
    }

    /**
     * Returns a key that can be used to check the validity of a given identity ID.
     *
     * The key should be unique for each individual user, and should be persistent
     * so that it can be used to check the validity of the user identity.
     *
     * The space of such keys should be big enough to defeat potential identity attacks.
     *
     * This is required if [[User::enableAutoLogin]] is enabled.
     * @return string a key that is used to check the validity of a given identity ID.
     * @see validateAuthKey()
     */
    public function getAuthKey()
    {
        return $this->auth_key;
    }

    /**
     * Validates the given auth key.
     *
     * This is required if [[User::enableAutoLogin]] is enabled.
     * @param string $authKey the given auth key
     * @return bool whether the given auth key is valid.
     * @see getAuthKey()
     */
    public function validateAuthKey($authKey)
    {
        return $this->getAuthKey() === $authKey;
    }


    public function getPost(){
        return $this->hasMany(Post::className(),["user_id"=>"id"])->count();
    }

    public function getPosttitle(){
        return $this->hasMany(Post::className(),["user_id"=>"id"])->select(['id','title'])->asArray()->all();
    }
    public function getFans(){
        return $this->hasMany(User::className(), ['id' => 'fan_id'])
            ->viaTable('fans', ['star_id' => 'id'])
            ->select(['id','username'])
            ->asArray()->all();
    }
    public function getStars(){
        return $this->hasMany(User::className(), ['id' => 'star_id'])
            ->viaTable('fans', ['fan_id' => 'id'])
            ->select(['id','username'])
            ->asArray()->all();
    }



}
